package model;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Server implements MstProtocal {
	Socket s;
	ObjectInputStream ois;
	ObjectOutputStream oos;

	public Server() throws IOException, SQLException, ClassNotFoundException {
		ServerSocket ss = new ServerSocket(12345);
		while (true) {
			s = ss.accept();
			ois = new ObjectInputStream(s.getInputStream());
			oos = new ObjectOutputStream(s.getOutputStream());
			int command = ois.readInt();
			if (command == USERLOGIN1) {
				user_login1();

			}
			if (command == USERLOGIN2) {
				user_login2();
			}
			if (command == USERADDUSER) {
				user_addUser();
			}

			if (command == USERUPDATEJIANLI) {
				user_updatejianli();
			}
			if (command == USERSEARCHJIANLI) {
				uesr_searchJianLi();
			}
			if (command == USERVIEWJIANLI) {
				user_viewJianLi();
			}
			if (command == USERADDJIANLI) {
				user_addjianli();
			}
			if (command == USERSEARCHCOM) {
				uesr_searchCom();
			}
			if (command == USERGETJIANLI) {
				uesr_getJianLi();
			}
			if (command == USERREVOKEJIANLI) {
				user_revokeJianli();
			}
			if (command == USERGETCOM1) {
				uesr_getCom1();
			}

			if (command == USERADDAPPLICANT) {
				user_addApplicant();
			}
			if (command == USERGETCOM2) {
				uesr_getCom2();
			}
			if (command == USERGETINFOR) {
				User_getiInfor();
			}
			if (command == USERVIEWINFOR) {
				User_viewInfor();
			}
			if (command == USERISSETJIANLI) {
				user_issetjianli();
			}
			if (command == USERSEARCHISSETJIANLIL) {
				uesr_searchissetjianli();
			}

			if (command == USERGETUSER) {
				uesr_getuser();
			}
			if (command == MANAGELOGIN1) {
				manage_login1();
			}
			if (command == MANAGELOGIN2) {
				manage_login2();
			}
			if (command == MANAGEDELETEUSER) {
				manage_deleteUser();
			}
			if (command == MANAGEDELETECOM) {
				manage_deleteCom();
			}
			if (command == MANAGESEARCHUSER) {
				manage_searchUser();
			}
			if (command == MANAGESEARCHUSER) {
				manage_searchcompany();
			}
			if (command == MANAGESEARCHEXAMINER) {
				manage_searchexaminer();
			}
			if (command == MANAGEDELETEEXAMINER) {
				manage_deleteExaminer();
			}
			if (command == MANAGEGETUSER) {
				manage_getUser();
			}
			if (command == MANAGEGETEXAMINER) {
				manage_getExaminer();
			}
			if (command == MANAGEVIEWJIANLI) {
				manage_view_jianli();
			}
			if (command == MANAGEVIEWCOM) {
				manage_view_company();
			}
			if (command == MANAGEGETCOM) {
				manage_get_company();
			}
			if (command == EXAMINERLOGIN1) {
				Examiner_login1();
			}
			if (command == EXAMINERLOGIN2) {
				Examiner_login2();
			}
			if (command == EXAMINERVIEWUSER) {
				Examiner_viewUser();
			}
			if (command == EXAMINERSEARCHUSER) {
				Examiner_searchUser();
			}
			if (command == EXAMINERGETUSER) {
				Examiner_getUser();
			}
			if (command == EXAMINERVIEWJIANLI) {
				Examiner_viewJianLi();
			}
			if (command == EXAMINERSEARCHEXAM) {
				Examiner_searchexam();
			}
			if (command == EXAMINERADDEXAM) {
				examiner_addexam();
			}
			if (command == COMPANYLOGIN1) {
				company_login1();
			}
			if (command == COMPANYLOGIN2) {
				company_login2();
			}
			if (command == COMPANYADDCOM) {
				company_addcompany();
			}
			if (command == COMPANYSEARCHUSER) {
				Company_searchUser();
			}
			if (command == COMPANYGETCOM) {
				company_getCom();
			}
			if (command == COMPANYGETINFORMATION) {
				company_get_information();
			}
			if (command == COMPANYVIEWINFORM) {
				company_view_information();
			}
			if (command == COMPANYUPDATEINFORM) {
				company_updateInform();
			}
			if (command == COMPANYSEARCHAPPLICANT) {
				company_searchApplicant();
			}

		}
	}

	User u = new User();
	Manage n = new Manage();
	Examiner e = new Examiner();
	Company y = new Company();

	private void user_login1() throws IOException, SQLException,ClassNotFoundException {
//		String id = ois.readUTF();
//		String pw = ois.readUTF();
//
//		User u = new User();
//		System.out.println(new User().login1(id, pw));
//		boolean active = false;
//		String sql = "select * from user where uid = "+id+" and upw = '"+pw+"'";
//		if (u != null)
//			active = true;
//		System.out.println(active);
//		oos.writeObject(active);
//		oos.flush();
		
		String uid = ois.readUTF();
	    String upw = ois.readUTF();
	    boolean result =  false;
	    String sql =  "select * from user where uid = '"+uid+"' and upw = "+upw+"";
	    ResultSet rs = DataConection.getStat().executeQuery(sql);
	    if(rs.next())
	    	result = true;
	    oos.writeBoolean(result);
	    oos.flush();
		
	}

	private void user_login2() throws IOException, SQLException {
		// TODO Auto-generated method stub
//		String email = ois.readUTF();
//		String pw = ois.readUTF();
//
//		User u = new User();
//		System.out.println(new User().login2(email, pw));
//		boolean active = false;
//		if (u != null)
//			active = true;
//		System.out.println(active);
//		oos.writeObject(active);
//		oos.flush();
		
		String uemail = ois.readUTF();
	    String mpw = ois.readUTF();
	    boolean result =  false;
	    String sql =  "select * from user where uemail = '"+uemail+"' and upw = "+mpw+"";
	    ResultSet rs = DataConection.getStat().executeQuery(sql);
	    if(rs.next())
	    	result = true;
	    oos.writeBoolean(result);
	    oos.flush();
	}

	private void user_addUser() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		
//		User s = (User) ois.readObject();
//		String sql = "insert into user(uid,uname,upw,uemail)values('"+s.getUid()+"','"+
//		s.getUname()+"',"+s.getUpw()+",'"+s.getUemail()+"')";
//
//		u.addUser(s);
		String uid = ois.readUTF();
	    String uname = ois.readUTF();
	    String upw = ois.readUTF();
	    String uemail = ois.readUTF();
	    boolean result =  false;
	    String sql =   "insert into user values('"+uid+"','"+
		uname+"',"+upw+",'"+uemail+"')";
	    ResultSet rs = DataConection.getStat().executeQuery(sql);
	    if(rs.next())
	    	result = true;
	    oos.writeBoolean(result);
	    oos.flush();

		

	}

	private void user_updatejianli() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		
		String jid = ois.readUTF();
		
		String jexpress = ois.readUTF();
		
		
		String jposition = ois.readUTF();
		
		u.updatejianli(jid, jexpress, jposition);

	}

	private void uesr_searchJianLi() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String msg = ois.readUTF();
		ArrayList<Jianli> jianlis = u.searchJianli(msg);
		oos.writeObject(jianlis);
		oos.flush();
	}

	private void user_viewJianLi() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String jid = ois.readUTF();
		Jianli jianlis = u.viewjianli(jid);
		oos.writeObject(jianlis);
		oos.flush();
	}

	private void user_addjianli() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		Jianli j = (Jianli) ois.readObject();
		u.addJianli(j);

	}

	private void uesr_searchCom() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String msg = ois.readUTF();
		ArrayList<Company> companys = u.searchCom(msg);
		oos.writeObject(companys);
		oos.flush();
	}

	private void uesr_getJianLi() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String jid = ois.readUTF();
		ArrayList<String> details = u.getJianli(jid);
		oos.writeObject(details);
		oos.flush();
	}

	private void user_revokeJianli() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String jid = (String) ois.readObject();
		u.revokeJianli(jid);

	}

	private void uesr_getCom1() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String cid = ois.readUTF();
		ArrayList<String> details = u.getCom1(cid);
		oos.writeObject(details);
		oos.flush();
	}

	private void uesr_getCom2() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String cid = ois.readUTF();
		ArrayList<String> details = u.getCom2(cid);
		oos.writeObject(details);
		oos.flush();
	}

	private void user_addApplicant() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		Applicant a = (Applicant) ois.readObject();
		u.addApplicant(a);
	}

	private void User_getiInfor() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String iid = ois.readUTF();
		ArrayList<String> details = u.getInfor(iid);
		oos.writeObject(details);
		oos.flush();
	}

	private void User_viewInfor() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String iid = ois.readUTF();
		Information informations = u.viewInfor(iid);
		oos.writeObject(informations);
		oos.flush();
	}

	private void user_issetjianli() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		Issetjianli i = (Issetjianli) ois.readObject();
		u.issetJianli(i);
	}

	private void uesr_searchissetjianli() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String msg = ois.readUTF();
		ArrayList<Issetjianli> issetjianlis = u.searchIssetjianli(msg);
		oos.writeObject(issetjianlis);
		oos.flush();
	}

	private void uesr_getuser() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String uid = ois.readUTF();
		ArrayList<String> details = u.getJianli(uid);
		oos.writeObject(details);
		oos.flush();
	}

	private void manage_login1() throws IOException, SQLException,
			ClassNotFoundException {
//		String id = ois.readUTF();
//		String pw = ois.readUTF();
//
//		System.out.println(new Manage().login1(id, pw));
//		boolean active = false;
//		if (n != null)
//			active = true;
//		System.out.println(active);
//		oos.writeObject(active);
//		oos.flush();
		String mid = ois.readUTF();
	    String mpw = ois.readUTF();
	    boolean result =  false;
	    String sql = "select * from manage where mid = "+mid+" and mpw = '"+mpw+"'";
	    ResultSet rs = DataConection.getStat().executeQuery(sql);
	    if(rs.next())
	    	result = true;
	    oos.writeBoolean(result);
	    oos.flush();
	    
	    

	}

	private void manage_login2() throws IOException, SQLException {
		// TODO Auto-generated method stub
//		String email = ois.readUTF();
//		String pw = ois.readUTF();
//
//		System.out.println(new Manage().login2(email, pw));
//		boolean active = false;
//		if (n != null)
//			active = true;
//		System.out.println(active);
//		oos.writeObject(active);
//		oos.flush();
		
		String memail = ois.readUTF();
	    String mpw = ois.readUTF();
	    boolean result =  false;
	    String sql =   "select * from manage where memail = '" + memail + "' and mpw = "+mpw+"";
	    ResultSet rs = DataConection.getStat().executeQuery(sql);
	    if(rs.next())
	    	result = true;
	    oos.writeBoolean(result);
	    oos.flush();
	}

	private void manage_deleteUser() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String uid = (String) ois.readObject();
		u.revokeJianli(uid);

	}

	private void manage_deleteCom() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String cid = (String) ois.readObject();
		u.revokeJianli(cid);

	}

	private void manage_searchUser() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String msg = ois.readUTF();
		ArrayList<User> users = n.searchUser(msg);
		oos.writeObject(users);
		oos.flush();
	}

	private void manage_searchcompany() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String msg = ois.readUTF();
		ArrayList<Company> companys = n.searchCom(msg);
		oos.writeObject(companys);
		oos.flush();
	}

	private void manage_searchexaminer() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String msg = ois.readUTF();
		ArrayList<Examiner> examiners = n.searchExaminer(msg);
		oos.writeObject(examiners);
		oos.flush();
	}

	private void manage_deleteExaminer() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String eid = (String) ois.readObject();
		u.revokeJianli(eid);

	}

	private void manage_getUser() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String uid = ois.readUTF();
		ArrayList<String> details = n.getUser(uid);
		oos.writeObject(details);
		oos.flush();
	}

	private void manage_getExaminer() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String eid = ois.readUTF();
		ArrayList<String> details = n.getExaminer(eid);
		oos.writeObject(details);
		oos.flush();
	}

	private void manage_view_jianli() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String jid = ois.readUTF();
		ArrayList<String> details = n.viewJianli(jid);
		oos.writeObject(details);
		oos.flush();
	}

	private void manage_view_company() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String iid = ois.readUTF();
		ArrayList<String> details = n.viewCom(iid);
		oos.writeObject(details);
		oos.flush();
	}

	private void manage_get_company() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String cid = ois.readUTF();
		ArrayList<String> details = n.getCom(cid);
		oos.writeObject(details);
		oos.flush();
	}

	private void Examiner_login1() throws IOException, SQLException,
			ClassNotFoundException {
//		String id = ois.readUTF();
//		String pw = ois.readUTF();
//
//		System.out.println(new Examiner().login1(id, pw));
//		boolean active = false;
//		if (e != null)
//			active = true;
//		System.out.println(active);
//		oos.writeObject(active);
//		oos.flush();
		
		String eid = ois.readUTF();
	    String epw = ois.readUTF();
	    boolean result =  false;
	    String sql = "select * from examiner where eid = "+eid+" and epw = '"+epw+"'";
	    ResultSet rs = DataConection.getStat().executeQuery(sql);
	    if(rs.next())
	    	result = true;
	    oos.writeBoolean(result);
	    oos.flush();
	}

	private void Examiner_login2() throws IOException, SQLException {
		// TODO Auto-generated method stub
//		String email = ois.readUTF();
//		String pw = ois.readUTF();
//
//		System.out.println(new Examiner().login2(email, pw));
//		boolean active = false;
//		if (e != null)
//			active = true;
//		System.out.println(active);
//		oos.writeObject(active);
//		oos.flush();
		
		String eemail = ois.readUTF();
	    String epw = ois.readUTF();
	    boolean result =  false;
	    String sql = "select * from examiner where eemail = '"+eemail+" 'and epw = "+epw+"";
	    ResultSet rs = DataConection.getStat().executeQuery(sql);
	    if(rs.next())
	    	result = true;
	    oos.writeBoolean(result);
	    oos.flush();

	}

	private void Examiner_viewUser() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String uid = ois.readUTF();
		User users = e.viewUser(uid);
		oos.writeObject(users);
		oos.flush();
	}

	private void Examiner_searchUser() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String msg = ois.readUTF();
		User users = e.viewUser(msg);
		oos.writeObject(users);
		oos.flush();
	}

	private void Examiner_getUser() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String uid = ois.readUTF();
		ArrayList<String> details = e.getUser(uid);
		oos.writeObject(details);
		oos.flush();
	}

	private void Examiner_viewJianLi() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String jid = ois.readUTF();
		ArrayList<String> details = e.viewJianli(jid);
		oos.writeObject(details);
		oos.flush();
	}

	private void Examiner_searchexam() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String msg = ois.readUTF();
		ArrayList<Exam> exams = e.searchExam(msg);
		oos.writeObject(exams);
		oos.flush();
	}

	private void examiner_addexam() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub

		Exam m = (Exam) ois.readObject();
		e.addExam(m);
	}

	private void company_login1() throws IOException, SQLException,
			ClassNotFoundException {
//		String id = ois.readUTF();
//		String pw = ois.readUTF();
//
//		System.out.println(new Company().login1(id, pw));
//		boolean active = false;
//		if (y != null)
//			active = true;
//		System.out.println(active);
//		oos.writeObject(active);
//		oos.flush();
		
		String cid = ois.readUTF();
	    String cpw = ois.readUTF();
	    boolean result =  false;
	    String sql = "select * from Company where cid = "+cid+" and cpw = '"+cpw+"'";
	    ResultSet rs = DataConection.getStat().executeQuery(sql);
	    if(rs.next())
	    	result = true;
	    oos.writeBoolean(result);
	    oos.flush();
	}

	private void company_login2() throws IOException, SQLException {
		// TODO Auto-generated method stub
//		String email = ois.readUTF();
//		String pw = ois.readUTF();
//
//		System.out.println(new Company().login2(email, pw));
//		boolean active = false;
//		if (y != null)
//			active = true;
//		System.out.println(active);
//		oos.writeObject(active);
//		oos.flush();
		
		String cemail = ois.readUTF();
	    String cpw = ois.readUTF();
	    boolean result =  false;
	    String sql = "select * from Company where cemail = '"+cemail+"' and cpw = "+cpw+"";
	    ResultSet rs = DataConection.getStat().executeQuery(sql);
	    if(rs.next())
	    	result = true;
	    oos.writeBoolean(result);
	    oos.flush();

	}

	private void company_addcompany() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub

		Company c = (Company) ois.readObject();
		y.addCom(c);
	}

	private void Company_searchUser() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String msg = ois.readUTF();
		ArrayList<User> users = y.searchUser(msg);
		oos.writeObject(users);
		oos.flush();
	}

	private void company_getCom() throws IOException, ClassNotFoundException,
			SQLException {
		// TODO Auto-generated method stub
		String cid = ois.readUTF();
		ArrayList<String> details = y.getCom(cid);
		oos.writeObject(details);
		oos.flush();
	}

	private void company_get_information() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String iid = ois.readUTF();
		ArrayList<String> details = y.getInformation(iid);
		oos.writeObject(details);
		oos.flush();
	}

	private void company_view_information() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String iid = ois.readUTF();
		Information informations = y.viewInform(iid);
		oos.writeObject(informations);
		oos.flush();
	}

	private void company_updateInform() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String iid = ois.readUTF();
		String izhiwei = ois.readUTF();
		String irequire = ois.readUTF();
		String imoney = ois.readUTF();
		String imainwork = ois.readUTF();
		y.updateInform(iid, izhiwei, irequire, imoney, imainwork);

	}

	private void company_searchApplicant() throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		String msg = ois.readUTF();
		ArrayList<Applicant> applicants = y.searchApplicant(msg);
		oos.writeObject(applicants);
		oos.flush();
	}

	public static void main(String[] args) {
		try {
			System.out.println("mst server starting");
			new Server();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
